package com.androidcare;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class Recibidor extends BroadcastReceiver{
	@Override
	public void onReceive(Context context, Intent intent) {
		Intent intent_act = new Intent(context,Actualiza.class);
		PendingIntent actualizacion = PendingIntent.getService(context, (int)System.currentTimeMillis(), intent_act, PendingIntent.FLAG_UPDATE_CURRENT);
		Calendar act_cal = new GregorianCalendar();
		Calendar cur_cal = new GregorianCalendar();
        cur_cal.setTimeInMillis(System.currentTimeMillis());
		act_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
        act_cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
        act_cal.set(Calendar.HOUR_OF_DAY, 23);
        act_cal.set(Calendar.MINUTE, 59);
        act_cal.set(Calendar.SECOND, 0);
        act_cal.set(Calendar.MILLISECOND, 0);
        act_cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
        act_cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
        
        long interval=24*60*60*1000; //intervalo de 24 horas
		AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);	
   	 	alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, act_cal.getTimeInMillis(), interval, actualizacion);
   	 	Log.i("Inicio","Actualizacion Programada");	
		Log.i("AppInfo","Actualizacion programada");
		
		Intent activity = new Intent(context, Inicio.class);
		activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(activity);
		
	}
}